﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using C1.Phone.Maps;
using Microsoft.Phone.Shell;

namespace ControlExplorer.Controls.Maps
{
    public partial class CustomTiles
        : UserControl, IExposeApplicationBarItems
    {
        ApplicationBarMenuItem mnuOpenStreet = new ApplicationBarMenuItem("open street maps");
        ApplicationBarMenuItem mnuCloudMade = new ApplicationBarMenuItem("cloud made maps");

        public CustomTiles()
        {
            InitializeComponent();

            MapsHelper.CheckConnection();

            maps.Source = null;
            mnuOpenStreet.Click += selectOpenStreet;
            mnuCloudMade.Click += selectCloudMade;

            Loaded += CustomTiles_Loaded;
        }

        void CustomTiles_Loaded(object sender, RoutedEventArgs e)
        {
            selectOpenStreet(null, EventArgs.Empty);
        }

        void selectCloudMade(object sender, EventArgs e)
        {
            mnuOpenStreet.IsEnabled = true;
            mnuCloudMade.IsEnabled = false;
            maps.Source = new CloudMadeMapsSource("02b70c6aaa04551bb29efa28cfd6e796") { Style = "999" };
        }

        void selectOpenStreet(object sender, EventArgs e)
        {
            mnuOpenStreet.IsEnabled = false;
            mnuCloudMade.IsEnabled = true;
            maps.Source = new OpenStreetMapsSource();
        }

        #region IExposeApplicationBarItems Members

        public IEnumerable<IApplicationBarMenuItem> ApplicationBarItems
        {
            get 
            {
                yield return mnuOpenStreet;
                yield return mnuCloudMade;
            }
        }

        #endregion
    }


    public class OpenStreetMapsSource : C1MultiScaleTileSource
    {
        private readonly string[] tilePathPrefixes = new[] { "a", "b", "c" };
        private readonly Random rand = new Random();
        private const string uriFormat = @"http://{S}.tile.openstreetmap.org/{Z}/{X}/{Y}.png";

        public OpenStreetMapsSource()
            : base(0x8000000, 0x8000000, 0x100, 0x100, 0)
        { }

        protected override void GetTileLayers(int tileLevel, int tilePositionX, int tilePositionY, IList<object> sources)
        {
            if (tileLevel > 8)
            {
                var zoom = tileLevel - 8;
                var prefix = tilePathPrefixes[rand.Next(3)];
                var url = uriFormat;

                url = url.Replace("{S}", prefix);
                url = url.Replace("{Z}", zoom.ToString());
                url = url.Replace("{X}", tilePositionX.ToString());
                url = url.Replace("{Y}", tilePositionY.ToString());
                sources.Add(new Uri(url));
            }
        }
    }


    public class CloudMadeMapsSource : C1MultiScaleTileSource
    {
        readonly Random rand = new Random();
        const string uriFormat = @"http://b.tile.cloudmade.com/{0}/{1}/256/{2}/{3}/{4}.png";
        string _apiKey;

        public string Style = "1";

        public CloudMadeMapsSource(string apiKey)
            : base(0x8000000, 0x8000000, 256, 256, 0)
        {
            _apiKey = apiKey;
        }

        protected override void GetTileLayers(int tileLevel, int tilePositionX, int tilePositionY, IList<object> sources)
        {
            if (tileLevel > 8)
            {
                sources.Add(new Uri(string.Format(uriFormat, _apiKey, Style, tileLevel - 8, tilePositionX, tilePositionY)));
            }
        }
    }

}